require 'test/unit'
require 'treenode.rb'

class TreeNode_Design < Test::Unit::TestCase
    
    def test_TreeNode_Initialized_Contract
        myNodeValue = 'I_Like_Cake'
        
        myTreeNode = TreeNode.new(myNodeValue)
        
        assert_equal(myTreeNode.value, myNodeValue)
        assert_equal(myTreeNode.parentNode, nil)
        assert_equal(myTreeNode.childNodes, [])
    end
    
    def test_Add_ChildNodes_And_Check_Total_Nodes
        myTreeNode = TreeNode.new('root')
        
        myTreeNode.addChild(TreeNode.new('ice_cream'))
        myTreeNode.addChild(TreeNode.new('pizza'))
        
        puts "childNodes="
        myTreeNode.childNodes.each { |node| print "\t" + node.value + "\n" }
        
        assert_not_equal(myTreeNode.childNodes, [])
        assert_equal(myTreeNode.childNodes.length, 2)
        assert_equal(myTreeNode.parentNode, nil)
    end
    
    def test_Add_ChildNodes_To_ChildNodes
        myTreeNode = TreeNode.new('root')
        
        myTreeNode.addChild(TreeNode.new('parent'))
        
        myChildMagicString = 'child'
        myTreeNode.childNodes[0].addChild(TreeNode.new(myChildMagicString))
        
        assert_equal(myChildMagicString, myTreeNode.childNodes[0].childNodes[0].value)
    end
    
    def test_ParentNodes_After_Adding_ChildNodes
        myTreeNode = TreeNode.new('root')
        
        myTreeNode.addChild(TreeNode.new('parent'))
        
        assert_equal('root', myTreeNode.childNodes[0].parentNode.value)

        myTreeNode.childNodes[0].addChild(TreeNode.new('child'))
        
        assert_equal('parent', myTreeNode.childNodes[0].childNodes[0].parentNode.value)

        myTreeNode.childNodes[0].childNodes[0].addChild(TreeNode.new('child1'))
        
        assert_equal('child', myTreeNode.childNodes[0].childNodes[0].childNodes[0].parentNode.value)
    end
end